//
//  ViewController.m
//  RectIntersect
//
//  Created by iD Student on 7/9/13.
//  Copyright (c) 2013 jasonkurtzman. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    gameTimer = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(gameUpdate) userInfo:nil repeats:YES];
	// Do any additional setup after loading the view, typically from a nib.
}

-(IBAction) upButtonClicked
{
    goingUp = true;
}
-(IBAction) downButtonClicked
{
    goingDown = true;
}
-(IBAction) leftButtonClicked
{
    goingLeft = true;
}
-(IBAction) rightButtonClicked
{
    goingRight = true;
}

-(IBAction) buttonReleased
{
    goingUp = false;
    goingDown = false;
    goingLeft = false;
    goingRight = false;
}


-(void)gameUpdate
{
    CGRect lavaRectangle = lavaImage.frame;
    CGRect playerRectangle = playerImage.frame;
    
    if(goingUp == true)
    {
        CGPoint oldCenter = lavaImage.center;
        lavaImage.center = CGPointMake(oldCenter.x, oldCenter.y - 3);
    }else if(goingDown == true)
    {
        CGPoint oldCenter = lavaImage.center;
        lavaImage.center = CGPointMake(oldCenter.x, oldCenter.y + 3);
    }else if(goingRight == true)
    {
        CGPoint oldCenter = lavaImage.center;
        lavaImage.center = CGPointMake(oldCenter.x + 3, oldCenter.y);
    }else if(goingLeft == true)
    {
        CGPoint oldCenter = lavaImage.center;
        lavaImage.center = CGPointMake(oldCenter.x - 3, oldCenter.y);
    }
    
    if(CGRectIntersectsRect(lavaRectangle, playerRectangle))
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Collision!" message:@"You Collided" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
        [alert show];
        lavaImage.center = CGPointMake(45, 45);
        [self buttonReleased];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
